home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / edit / pt20pc.zip / INDEXHLP.C < prev    next >
C/C++ Source or Header  |  1991-02-04  |  2KB  |  83 lines

  1. #include "stdio.h"
  2.  
  3. #define NSCREENS 256
  4. #define BUFFERSIZE 512
  5.  
  6. char buffer[BUFFERSIZE];
  7. long screens[NSCREENS];
  8.  
  9. main(argc, argv)
  10.     char *argv[];
  11. {
  12.     char ch, *s, *fgets(), fromFile[80], toFile[80];
  13.     int i, len, n;
  14.     long cp, ftell();
  15.     FILE *fp, *fout;
  16.     
  17.     if( argc < 2 ) {
  18.         printf("Raw help file to index: ");
  19.         scanf("%s", fromFile);
  20.     } else
  21.         strcpy(fromFile, argv[1]);
  22.  
  23.     if( argc < 3 ) {
  24.         printf("Indexed help file to write: ");
  25.         scanf("%s", toFile);
  26.     } else
  27.         strcpy(toFile, argv[2]);
  28.     printf("\nIndexing file «%s» into file «%s»\n\n",
  29.         fromFile, toFile);
  30.  
  31.     fp = fopen(fromFile, "rb");
  32.     if( fp == NULL ) {
  33.         cprintf("Could not open %s\n", fromFile);
  34.         exit(2);
  35.     }
  36.  
  37.     /* initialize */
  38.     for(i = 0; i < NSCREENS; ++i)
  39.         screens[i] = 0L;
  40.  
  41.     printf("Scanning %s to find all the screens defined ...\n", fromFile);
  42.     /* find all the screen and their offsets */
  43.     while( 1 ) {
  44.         cp = ftell(fp);
  45.         s = fgets(buffer, BUFFERSIZE, fp);
  46.         if( s == NULL )    /* end of file */
  47.             break;
  48.         if( *s == '\\' ) {
  49.             ch = *++s;
  50.             if( '0' <= ch && ch <= '9' ) {
  51.                 n = atoi(s);
  52.                 if( n > NSCREENS )
  53.                     cprintf("screen %d out of range\r\n",
  54.                         n);
  55.                 else
  56.                     screens[n] = cp+8*NSCREENS;
  57.             }
  58.         }
  59.     }
  60.     fout = fopen(toFile, "wb");
  61.     if( fout == NULL ) {
  62.         cprintf("Could not open %s\r\n", toFile);
  63.         exit(3);
  64.     }
  65.  
  66.     printf("Writing the screen index to %s\n", toFile);
  67.     for(i = 0; i < NSCREENS; ++i)
  68.         fprintf(fout, "%6ld\r\n", screens[i]);
  69.     rewind(fp);
  70.  
  71.     printf("Writing the help screens to %s\n", toFile);
  72.     while( 1 ) {
  73.         s = fgets(buffer, BUFFERSIZE, fp);
  74.         if( s == NULL )
  75.             break;
  76.         fputs(buffer, fout);
  77.     }
  78.     fclose(fp);
  79.     fclose(fout);
  80.     printf("Done  \n");
  81.     exit(0);
  82. }
  83.